<?php

namespace Admin\Controller;

class FriendController extends AdminController
{

    public function upload($inputName)
    {
        $upload           = new \Think\Upload(); // 实例化上传类
        $upload->maxSize  = 3145728; // 设置附件上传大小
        $upload->exts     = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        // 上传单个文件
        $info = $upload->uploadOne($_FILES[$inputName]);
        if (!$info) {
// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {
// 上传成功 获取上传文件信息
            return $info['savepath'] . $info['savename'];
        }
    }
     public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        if( in_array(C('USER_ADMINISTRATOR'), $id)){
            $this->error("不允许对超级管理员执行该操作!");
        }
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'hide':
                $this->forbid('Friend', $map );
                break;
            case 'show':
                $this->resume('Friend', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }
    public function index()
    {
        $title         = I('title');
        
        if (is_numeric($title)) {
            $map['id|title'] = array(intval($title), array('like', '%' . $title . '%'), '_multi' => true);
        } else {
            $map['title'] = array('like', '%' . (string) $title . '%');
        }

        $list = $this->lists('friend', $map);
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '友链管理 ';
        $this->display();

    }
    public function add()
    {
        if (IS_POST) {
            $_POST['pic'] = $this->upload('pic');
            $form         = D('Friend');
            $data         = $form->create();
            if ($data) {
                $form->add($data);
                $this->success('添加成功', U('index'));
            } else {
                $this->error($form->getError());
            }
        } else {
            $this->display();
        }
    }

    public function update()
    {
        $form = D('Friend');
        if (IS_POST) {
            if ($_FILES['pic']['error'] != 4) {
                $_POST['pic'] = $this->upload('pic');
            }
            $data = $form->create();
            if ($data) {
                $res = $form->where(['id' => I('post.id')])->save($data);
                $this->success('修改成功', U('Friend/index'));
            } else {
                $this->error($form->getError());
            }
        } else {
            $res = $form->find(I('get.id'));
            $this->assign('res', $res);
            $this->display();
        }

    }

     public function del(){
        $form = D('Friend');

     $res=$form->where(['id' => I('get.id')])->delete();
     if ($res) {
         $this->success('删除成功',U('Friend/index'));
     }else{
        $this->error('删除失败');
     }


     }







}
